<template>
  <el-dialog 
    class="edit-dialog"
    :visible.sync="isVisible"
    :close-on-click-modal="false"
    :title="title"
    width="600px">
    <el-form
      ref="editForm"
      :model="editForm"
      :rules="rules"
      autocomplete="off"
      label-position="right" 
      label-width="100px"
    >
      <el-form-item prop="userName" label="用户名">
        <el-input
          ref="userName"
          v-model="editForm.userName"
          placeholder="请输入用户名"
          name="userName"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="roleId" label="所属角色">
        <el-select v-model="editForm.roleId" placeholder="请选择角色" width="100%">
          <el-option
            v-for="item in roleItems"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          ref="password"
          v-model="editForm.password"
          placeholder="请输入密码"
          name="password"
          type="password"
        />
      </el-form-item>
      <el-form-item prop="mobile" label="手机号">
        <el-input
          ref="mobile"
          v-model="editForm.mobile"
          placeholder="请输入手机号"
          name="mobile"
          type="text"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="medium" @click="isVisible=false">取 消</el-button>
      <el-button size="medium" type="primary" :loading="submitLoading" @click="editSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {insertUser, updateUser,selectRolePage } from '@/http/apiUrls.js';
export default {
  name: 'UpdateForm',
  props: {
    // 弹窗显示
    dialogVisible: { type: Boolean, default: false},
    // 弹窗类型
    type: { type: String, default: "add" }
  },
  computed: {
    // 控制显示
    isVisible: {
      get() { 
        return this.dialogVisible; 
      },
      set() {
        this.$refs['editForm'].resetFields();
        this.editForm = this.initEditObj()
        this.$emit('close')
      }
    },
    // 标题
    title:{
      get(){
        if(this.type == 'add'){
          return '添加用户'
        }else{
          return '修改用户'
        }
      }
    }
  },
  mounted(){
    this.getRoleItems()
  },
  data() {
    return {
      submitLoading:false,
      editForm: this.initEditObj(),
      roleItems: [],
      rules: {
        userName: {
          required: true,
          message: '请输入用户名',
          trigger: "blur"
        },
        mobile: {
          required: true,
          message: '请输入电话号码',
          trigger: "blur"
        },
        roleId: {
          required: true,
          message: '请选择所属角色',
          trigger: "blur"
        },
        password: {
          required: true,
          message: '请输入密码',
          trigger: "blur"
        }
      },
      
    }
  },
  methods: {
    // 修改传递值
    setEditObj(val) {
      console.log(val);
      this.editForm = {...val};
    },
    // 重置值
    initEditObj(){
      return {
        userName: "",
        mobile: "",
        password: "",
        roleId: ''
      }
    },
    getRoleItems(){
      let requestData = {
        current: 1,
        size : 100,
        model: {}
      }
      this.$postJson(selectRolePage, requestData).then(res => {
        if(res){
          this.roleItems = res.records;
        }
      })
    },
    editSubmit(){
       this.$refs.editForm.validate(valid => {
        if (valid) {
          if(this.type === 'add'){
            this.insert()
          }else{
            this.edit()
          }
        } else {
          return false;
        }
      });
    },
    insert(){
      let requestData = this.editForm;
      this.submitLoading = true;
      this.$postJson(insertUser, requestData).then(res => {
        this.submitLoading = false;
        if(res){
          this.$message({
            type: 'success',
            message: '添加成功!'
          });
          this.isVisible = false;
          this.$emit('success')
        }
      })
    },
    edit(){
      let requestData = this.editForm;
      this.submitLoading = true;
      this.$postJson(updateUser, requestData).then(res => {
        this.submitLoading = false;
        if(res){
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.isVisible = false;
          this.$emit('success')
        }
      })
    }
  }
}
</script>
<style lang="scss">
.edit-dialog{
  .el-dialog__header{
    padding: 15px 20px 15px;
    .el-dialog__title{
      color: #fff;
    }

    i{
      color: #fff;
    }
    
    background-color: #006ACC;
  }

  .el-dialog__footer{
    padding: 15px 20px 15px;
    border-top: 1px solid #dfe4ea;
  }
}
</style>